package View.list;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.UIManager;
import javax.swing.border.Border;

import Management.Animation;

public class MyListCellRenderer extends DefaultListCellRenderer implements MouseListener{
	private boolean isSelected = false;
	private boolean hasFocus = false;
	private boolean isEntered = false;
    private static final Color COLOR1 = new Color(153, 222, 253);
    private static final Color COLOR2 = new Color(213, 239, 252);
    private static final Color COLOR3 = new Color(241, 248, 253);
    private BufferedImage bufferedImage = null; //中间图像
    
    
	public MyListCellRenderer() {
		super();
//		setOpaque(false);
	}
    
	@Override
	public Component getListCellRendererComponent(JList list,
											       Object value,
											       int index,
											       boolean isSelected,
											       boolean cellHasFocus) {
		this.isSelected = isSelected;
		this.hasFocus = cellHasFocus;
	
		return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
	}
	
	
    /**
     * Paints the value.  The background is filled based on selected.
     */
   public void paint(Graphics g) {
		if (isSelected) {
		    int W = getWidth();
		    int H = getHeight();
		    float R = (float) Math.pow((W*W + H*H), 0.5);
	        
		    GradientPaint GP = new GradientPaint(0, 0, COLOR3, 0, getHeight(), COLOR2, true);
		    if(bufferedImage == null || bufferedImage.getHeight() != H || bufferedImage.getWidth() != W) {
		    	bufferedImage	= new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		    	Graphics2D g2d 	= bufferedImage.createGraphics();

		    	g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		    	g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
		    	g2d.setPaint(GP);
		    	g2d.fillRoundRect(0, 0, W - 2, H - 2, 10, 10);
		    	g2d.setColor(COLOR1);
		    	g2d.drawRoundRect(0, 0, W - 1, H - 1, 10, 10);
		    	g2d.dispose();
		    }
		    g.drawImage(bufferedImage, 0, 0, null);
		}

		super.paint(g);
   }

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
		repaint();
	}

	public void setHasFocus(boolean hasFocus) {
		this.hasFocus = hasFocus;
		repaint();
	}

	public void setEntered(boolean isEntered) {
		this.isEntered = isEntered;
		repaint();
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		if (arg0.getClickCount() == 1) {
			setSelected(true);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		setEntered(true);
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		setEntered(false);
	}
	
	@Override
	public void mousePressed(MouseEvent arg0) {
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		
	}
	
    public static void main(String[] args) {
    	JFrame jf =new JFrame();
    	
    	JList jl = new JList();
    	jl.setCellRenderer(new MyListCellRenderer());
    	
    	jf.setLayout(new BorderLayout());
    	jf.add("North", jl);
    	jf.setVisible(true);
    	jf.setSize(200,300);
    	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
